/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.kason.fixlength;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author 卢清波
 * @date 2021/2/3 15:48
 */
public class FixLengthServer
{
    public static void main(String[] args) throws InterruptedException
    {
        NioEventLoopGroup boss = new NioEventLoopGroup(1);
        NioEventLoopGroup worker = new NioEventLoopGroup();// 不指定线程数量，默认是当前cpu核心数的2倍
        try
        {
            // 定义server启动类
            ServerBootstrap ss = new ServerBootstrap();
            ss.group(boss,worker)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG,1024)
                    .childHandler(new ChannelInitializer<SocketChannel>()
                    {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception
                        {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast(new FixLengthDecode());
                            pipeline.addLast(new FixLengthServerHandler());
                        }
                    });
            // 绑定端口，注册channel监听事件,同步等待
            ChannelFuture channelFuture = ss.bind(9000).sync();
            channelFuture.channel().closeFuture().sync();
        }
        catch (Exception ex)
        {
            throw ex;
        }finally
        {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }

    }







}
